package com.kezesong.leasing.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
    static Logger logger = LoggerFactory.getLogger(CookieUtils.class);



    public static void WriteCookie(String cookieKey, String  cookieValue, HttpServletResponse httpServletResponse,String domain){
        logger.debug("========begin create cookie=====, cookieKey:{}, cookieValue:{}", cookieKey, cookieValue);

        Cookie cookie = new Cookie(cookieKey, cookieValue);
        cookie.setDomain(domain);
        cookie.setMaxAge(-1);
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
    }

    public static String getCookie(String cookieKey, HttpServletRequest httpServletRequest){
        String result="";
        Cookie[] cookies = httpServletRequest.getCookies();
        if (null != cookies){
            for (Cookie cookie: cookies){
                if (cookieKey.equals(cookie.getName())) {
                    result = cookie.getValue();
                }
            }
        }
        logger.debug("CookieUtils.getCookie  cookieKey:{}, result:{}",cookieKey, result);
        return result;
    }
}
